import 'package:flutter/material.dart';
import 'package:flutter_rider/main/widgets/pop/pop_car_status.dart';
import 'package:provider/provider.dart';
import '../main/widgets/ui_constant.dart';
import '../module/mine/provider/outside_car_provider.dart';
import 'alib.dart';

class OpenUtils {
  static void open(BuildContext context, Widget widget) {
    Alib.hideKeyboard(context);
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => widget,
      ),
    );
  }

  static void pop(BuildContext context, Widget widget) {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        return AnimatedPadding(
          child: widget,
          padding: MediaQuery.of(context).viewInsets,
          duration: const Duration(microseconds: 100),
        );
      },
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
    );
  }

  static void pop2(BuildContext context, Widget widget, [Color? barrierColor]) {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        var sh = MediaQuery.of(context).size.height;
        var height = sh * 0.7;
        if (MediaQuery.of(context).viewInsets.bottom > sh * 0.2) {
          height = sh * 0.9;
        }
        return AnimatedContainer(
          child: widget,
          padding: MediaQuery.of(context).viewInsets,
          height: height,
          duration: const Duration(microseconds: 100),
        );
      },
      backgroundColor: Colors.transparent,
      barrierColor: barrierColor,
      isScrollControlled: true,
    );
  }

  ///车辆管理弹出界面 高度只有0.35
  static void pop3(BuildContext context, Widget widget, [Color? barrierColor]) {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        var sh = MediaQuery.of(context).size.height;
        var height = sh * 0.35;
        if (MediaQuery.of(context).viewInsets.bottom > sh * 0.2) {
          height = sh * 0.55;
        }
        return AnimatedContainer(
          child: widget,
          padding: MediaQuery.of(context).viewInsets,
          height: height,
          duration: const Duration(microseconds: 100),
        );
      },
      backgroundColor: Colors.transparent,
      barrierColor: barrierColor,
      isScrollControlled: true,
    );
  }

  /// 高度由内部决定
  static void pop4(BuildContext context, Widget widget, [Color? barrierColor]) {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        var sh = MediaQuery.of(context).size.height;
        var height = sh * 0.7;
        if (MediaQuery.of(context).viewInsets.bottom > sh * 0.2) {
          height = sh * 0.9;
        }
        return AnimatedContainer(
          child: widget,
          padding: MediaQuery.of(context).viewInsets,
          // height: height,
          duration: const Duration(microseconds: 100),
        );
      },
      backgroundColor: Colors.transparent,
      barrierColor: barrierColor,
      isScrollControlled: true,
    );
  }

  static void popCars(BuildContext context, List cars, PopEnterType type) {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        var sh = MediaQuery.of(context).size.height;
        var height = sh * 0.7;
        if (MediaQuery.of(context).viewInsets.bottom > sh * 0.2) {
          height = sh * 0.9;
        }
        return AnimatedContainer(
          child: PageView.builder(
            itemBuilder: (context, index) {
              var car = cars[index];
              double percent = car["status"] == 3 ? 0.85 : 0.7;
              return PopCarStatus(car["id"], type, title: car["car_no"]);
            },
            itemCount: cars.length,
            controller: PageController(
              initialPage: 0,
              keepPage: false,
              viewportFraction: 0.9,
            ),
          ),
          padding: MediaQuery.of(context).viewInsets,
          height: height,
          duration: const Duration(microseconds: 100),
        );
      },
      backgroundColor: Colors.transparent,
      //barrierColor: Colors.transparent,
      isScrollControlled: true,
    );
  }

  /// 高度由内部决定 弹车牌键盘
  static void popKey(BuildContext context, Widget widget) {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        // var sh = MediaQuery.of(context).size.height;
        // var height = sh * 0.7;
        // if (MediaQuery.of(context).viewInsets.bottom > sh * 0.2) {
        //   height = sh * 0.9;
        // }
        return AnimatedContainer(
          child: widget,
          padding: MediaQuery.of(context).viewInsets,
          // height: height,
          duration: const Duration(microseconds: 100),
        );
      },
      backgroundColor: Colors.transparent,
      barrierColor: Colors.transparent,
      isScrollControlled: false,
    );
  }
}
